package com.live.dengdengtv.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.live.dengdengtv.BaseActivity;
import com.live.dengdengtv.R;
import com.live.dengdengtv.bean.AppInfo;
import com.live.dengdengtv.bean.SimpleInfoData;
import com.live.dengdengtv.bean.gson.TopicListItem;
import com.live.dengdengtv.net.NetTaskList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by wyf on 2016/9/24.
 */
public class AddTopicActivity extends BaseActivity implements View.OnClickListener{

    private ListView listView;

    private List<String> topicList = new ArrayList<String>();
    private EditText topic_et;
    private MyAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_add_topic);

        topic_et = (EditText) findViewById(R.id.topic_et);
        Button confirm_btn = (Button) findViewById(R.id.confirm_btn);
        confirm_btn.setOnClickListener(this);

        listView = (ListView) findViewById(R.id.listView);
        adapter = new MyAdapter();
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                topic_et.setText(topicList.get(position));
            }
        });

        getTopicList();
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.confirm_btn){
            String title = topic_et.getText().toString().trim();
            if(!TextUtils.isEmpty(title)){
                addTopic(title);
            }
        }
    }

    private void getTopicList(){
        HashMap<String,String> params = new HashMap<>();
        params.put("key","1");//1、最热 2、最多
//        params.put("class_id","1");//
        params.put("page_index","1");//
        params.put("page_count","10");//
        params.putAll(AppInfo.getHeadInfo());
        requestData(NetTaskList.topic_list, params, 1);
    }

    private void addTopic(String title){
        HashMap<String,String> params = new HashMap<>();
        params.put("title",title);
        params.putAll(AppInfo.getHeadInfo());
        requestData(NetTaskList.add_topic, params, 2);
    }


    @Override
    public boolean onResponse(SimpleInfoData response, int id) {
        if(response.result == 0){
            if (id == 1) {
                JsonArray array = response.data.getAsJsonArray();
                for (int i=0; i<array.size() ;i++){
                    TopicListItem liveItem = new Gson().fromJson(array.get(i).getAsJsonObject(), TopicListItem.class);
                    topicList.add(liveItem.title);
                }
                adapter.notifyDataSetChanged();
            }else if(id == 2){
                Intent intent  = new Intent();
                intent.putExtra("topic", topic_et.getText().toString().trim());
                setResult(Activity.RESULT_OK,intent);
                finish();
            }
        }
        return false;
    }

    private class MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return topicList.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView != null) {
                holder = (ViewHolder)convertView.getTag();
            } else {
                convertView = View.inflate(context, R.layout.item_topic, null);

                TextView textView = (TextView) convertView.findViewById(R.id.textView);
                holder = new ViewHolder();
                holder.topic_tv = textView;

                convertView.setTag(holder);
            }

            holder.topic_tv.setText("# "+topicList.get(position)+" #");
            return convertView;
        }
    }

    private static class ViewHolder{
        TextView topic_tv;
    }
}
